package com.fr.report;

import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.util.vt.VT4FR;
import com.fr.base.core.util.vt.VT4FR.FUNC;
import com.fr.data.TableData;
import com.fr.report.cellElement.Formula;
import com.fr.report.cellElement.core.DSColumn;
import com.fr.report.core.ParameterHelper;
import com.fr.report.core.ScriptUtils;
import com.fr.report.parameter.Parameter;
import com.fr.report.parameter.ParameterProcessor;
import com.fr.report.script.Calculator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class WorkBook extends AbstractTemplateWorkBook
{
  public WorkBook()
  {
  }

  public WorkBook(TemplateReport paramTemplateReport)
  {
    addReport(paramTemplateReport);
  }

  public void addReport(int paramInt, TemplateReport paramTemplateReport)
  {
    super.addReport(paramInt, null, paramTemplateReport);
  }

  public void addReport(int paramInt, String paramString, TemplateReport paramTemplateReport)
  {
    super.addReport(paramInt, paramString, paramTemplateReport);
  }

  public ResultWorkBook execute(Map paramMap)
  {
    apply4Parameters(paramMap);
    RWorkBook localRWorkBook = new RWorkBook();
    dealWithExecutedAttr(localRWorkBook);
    int i = 0;
    int j = getReportCount();
    while (i < j)
    {
      if ((i > 0) && (!(VT4FR.WORK_BOOK.support())))
        break;
      localRWorkBook.addReport(getReportName(i), getTemplateReport(i).execute(paramMap));
      ++i;
    }
    return localRWorkBook;
  }

  public ResultWorkBook execute4Write(Map paramMap)
  {
    apply4Parameters(paramMap);
    RWorkBook localRWorkBook = new RWorkBook();
    dealWithExecutedAttr(localRWorkBook);
    int i = 0;
    int j = getReportCount();
    while (i < j)
    {
      if ((i > 0) && (!(VT4FR.WORK_BOOK.support())))
        break;
      localRWorkBook.addReport(getReportName(i), getTemplateReport(i).execute4Write(paramMap));
      ++i;
    }
    return localRWorkBook;
  }

  public ResultWorkBook execute4Form(Map paramMap)
  {
    apply4Parameters(paramMap);
    RWorkBook localRWorkBook = new RWorkBook();
    dealWithExecutedAttr(localRWorkBook);
    int i = 0;
    int j = getReportCount();
    while (i < j)
    {
      if ((i > 0) && (!(VT4FR.WORK_BOOK.support())))
        break;
      localRWorkBook.addReport(getReportName(i), getTemplateReport(i).execute4Form(paramMap));
      ++i;
    }
    return localRWorkBook;
  }

  private void dealWithExecutedAttr(ResultWorkBook paramResultWorkBook)
  {
    if (paramResultWorkBook == null)
      return;
    Iterator localIterator = getTableDataNameIterator();
    while (localIterator.hasNext())
    {
      String str = (String)localIterator.next();
      paramResultWorkBook.putTableData(str, getTableData(str));
    }
    if (getReportWebAttr() != null)
      paramResultWorkBook.setReportWebAttr(getReportWebAttr());
    if (getReportExportAttr() != null)
      paramResultWorkBook.setReportExportAttr(getReportExportAttr());
  }

  public Parameter[] getParameters()
  {
    ArrayList localArrayList = new ArrayList();
    ParameterHelper.addGlobal_ParameterToList(localArrayList);
    ReportParameterAttr localReportParameterAttr = getReportParameterAttr();
    if ((localReportParameterAttr != null) && (localReportParameterAttr.getParameters() != null))
      localArrayList.addAll(Arrays.asList(localReportParameterAttr.getParameters()));
    int i = 0;
    int j = this.tableDataList.size();
    while (i < j)
    {
      TableData localTableData = (TableData)this.tableDataList.get(i);
      if ((localTableData != null) && (localTableData.getParameters() != null))
        localArrayList.addAll(Arrays.asList(localTableData.getParameters()));
      ++i;
    }
    return ((Parameter[])localArrayList.toArray(new Parameter[localArrayList.size()]));
  }

  public void apply4Parameters(Map paramMap)
  {
    ReportParameterAttr localReportParameterAttr = getReportParameterAttr();
    if (localReportParameterAttr != null)
    {
      Parameter[] arrayOfParameter = localReportParameterAttr.getParameters();
      for (int i = 0; i < arrayOfParameter.length; ++i)
        if (paramMap.get(arrayOfParameter[i].getName()) == null)
        {
          Object localObject = arrayOfParameter[i].getValue();
          if (localObject instanceof Formula)
            localObject = ScriptUtils.executeFormula(Calculator.createCalculator(), (Formula)localObject);
          paramMap.put(arrayOfParameter[i].getName(), localObject);
        }
      if (localReportParameterAttr.getParameterProcessor() != null)
        paramMap = localReportParameterAttr.getParameterProcessor().process(paramMap);
    }
  }

  public boolean renameTableData(String paramString1, String paramString2)
  {
    if (super.renameTableData(paramString1, paramString2))
    {
      int i;
      if (getTableData(paramString1) == null)
        for (i = 0; i < getReportCount(); ++i)
        {
          Report localReport = getReport(i);
          if (localReport != null)
          {
            Iterator localIterator = localReport.cellIterator();
            while (true)
            {
              DSColumn localDSColumn;
              do
              {
                Object localObject;
                do
                {
                  if (!(localIterator.hasNext()))
                    break label115;
                  CellElement localCellElement = (CellElement)localIterator.next();
                  localObject = localCellElement.getValue();
                }
                while (!(localObject instanceof DSColumn));
                localDSColumn = (DSColumn)localObject;
              }
              while (!(ComparatorUtils.equals(localDSColumn.getDSName(), paramString1)));
              localDSColumn.setDSName(paramString2);
            }
          }
        }
      label115: return true;
    }
    return false;
  }
}